package com.simpleaccount;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.simpleaccount.Common.SnackbarUtil;
import com.simpleaccount.Db.DbUtils;

import com.simpleaccount.Login.LoginActivity;
import com.simpleaccount.Toast.ToastUtils;
import com.simpleaccount.User.AdviceFeedbackActivity;
import com.simpleaccount.User.ChangepswActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;

import cn.pedant.SweetAlert.SweetAlertDialog;
import es.dmoral.toasty.Toasty;



/**
 * Created by zjx on 2017/7/15.
 */

public class SettingActivity extends Activity {
    @Bind(R.id.tv_setting)
    TextView tvSetting;
    @Bind(R.id.btn_cleanAllbills)
    RelativeLayout btnCleanAllbills;
    @Bind(R.id.btn_changePassword)
    RelativeLayout btnChangePassword;
    @Bind(R.id.btn_adviceFeedback)
    RelativeLayout btnAdviceFeedback;
    @Bind(R.id.btn_logout)
    Button btnLogout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {//2
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);

        BmobUser gUser = BmobUser.getCurrentUser();
        if (gUser == null) //判断用户是否登录
            btnLogout.setVisibility(View.INVISIBLE);
        else
            btnLogout.setVisibility(View.VISIBLE);
    }


    @OnClick({R.id.btn_cleanAllbills, R.id.btn_changePassword, R.id.btn_adviceFeedback, R.id.btn_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cleanAllbills:
                BmobUser myUser = BmobUser.getCurrentUser();
                if (myUser == null) { //判断用户是否登录
                    ToastUtils.toast(this,"请先登录","info");
                } else {
                    new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
                            .setTitleText("清空账单")
                            .setContentText("您即将进行一次无法逆转的操作！")
                            .setConfirmText("确认")
                            .setCancelText("对不起，我错了")
                            .showCancelButton(true)
                            .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sDialog) {
                                    sDialog.cancel();
                                }
                            })
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sDialog) {
                                    DbUtils.deleteAllEntries();
                                    sDialog
                                            .setTitleText("删除成功")
                                            .setContentText("您所有的账单已被清空")
                                            .setConfirmText("除了确认还有什么办法呢")
                                            .showCancelButton(false)
                                            .setConfirmClickListener(null)
                                            .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);


                                }
                            }).show();
//                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
//                    builder.setIcon(R.drawable.ic_launcher);//设置图标
//                    builder.setTitle("清除所有账单");//设置对话框的标题
//                    builder.setMessage("你确定要删除所有账单吗");
//                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            //在此处实现删除账单功能
//                            ToastUtils.toast(SettingActivity.this,"已清除所有账单","success");
//                        }
//                    });
//                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            //
//                        }
//                    });
//                    AlertDialog b = builder.create();
//                    b.show();
                }
                break;
            case R.id.btn_changePassword:
                BmobUser fUser = BmobUser.getCurrentUser();
                if (fUser == null) { //判断用户是否登录
                    ToastUtils.toast(this,"请先登录","info");
                } else {
                    Intent intent_changepsw = new Intent(SettingActivity.this, ChangepswActivity.class);
                    startActivity(intent_changepsw);
                    finish();
                }
                break;
            case R.id.btn_adviceFeedback:
                Intent intent_adFeedback = new Intent(SettingActivity.this, AdviceFeedbackActivity.class);
                startActivity(intent_adFeedback);
                break;
            case R.id.btn_logout:
                BmobUser.logOut();   //清除缓存用户对象
                Intent intent_logout = new Intent(SettingActivity.this, LoginActivity.class);
                startActivity(intent_logout);
                finish();
                break;
        }
    }

    public interface Callback {
        public void logout();
    }

    @Override
    protected void onPause() {
        super.onPause();
        ToastUtils.cancel();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ToastUtils.cancel();
    }

    @Override
    protected void onStop() {
        super.onStop();
        ToastUtils.cancel();
    }

}

